﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace AcessaDadosBD.Repositorios
{
    public class TransacaoRepository : IRepository<Transacao>
    {
        private IDbCommand Command { get; set; }

        public TransacaoRepository(IDbCommand cmd)
        {
            this.Command = cmd;
        }

        public int Insert(Transacao entity)
        {
            try
            {
                Command.Connection.Open();

                Command.CommandText = "INSERT INTO tbTransacao (dataTransacao, tipoTransacao, idOperadoraPrestador) VALUES ('" +
                    entity.DataTransacao.ToUniversalTime() + "'," + (int)entity.TipoTransacao + "," + entity.IDOperadoraPrestador +  ")";
                Command.CommandType = CommandType.Text;

                Command.ExecuteNonQuery();

                Command.CommandText = "SELECT @@IDENTITY";

                return int.Parse(Command.ExecuteScalar().ToString());
            }
            catch (Exception e)
            {
                throw new Exception("Erro ao executar operação: " + e.Message);
            }
            finally
            {
                Command.Connection.Close();
            }
        }

        public Transacao GetById(int id)
        {
            throw new Exception("Not implemented method!");
        }

        public IQueryable<Transacao> GetAll()
        {
            throw new Exception("Not implemented method!");
        }

        public void Delete(Transacao entity)
        {
            //throw new Exception("Not implemented method!");
        }

        public void Update(Transacao entity)
        {
            //throw new Exception("Not implemented method!");
        }
    }
}
